﻿Imports printApodiksiC

Public Class PoliseisControl

    Private Sub makeDataGrid()
        DataGridView1.Columns.Add("proion", "Προϊόν")
        DataGridView1.Columns.Add("onomaP", "Όνομα Προϊόντος")
        DataGridView1.Columns.Add("quant", "Ποσότητα")
        DataGridView1.Columns.Add("price", "Τιμή Μονάδας")
        DataGridView1.Columns.Add("pricesinolo", "Συνολική Τιμή")
    End Sub

    Private Function keepetaira() As String
        Dim fileReader As String = "none"
        Try
            fileReader = My.Computer.FileSystem.ReadAllText(Application.StartupPath & _
                                            "\keppetairia.txt")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return fileReader
    End Function
    Private Sub PoliseisBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim PLadapter As hard_oneDataSetTableAdapters.proiontaTableAdapter = New hard_oneDataSetTableAdapters.proiontaTableAdapter
        Dim PLtable As hard_oneDataSet.proiontaDataTable = New hard_oneDataSet.proiontaDataTable

        PLadapter.FillBy(PLtable, keepetaira())
        ProiontaComboBox.DataSource = PLtable
        ProiontaComboBox.DisplayMember = "idProionta"
        ProiontaComboBox.ValueMember = "idProionta"


        Dim categoryadapter1 As hard_oneDataSetTableAdapters.pelatesTableAdapter = New hard_oneDataSetTableAdapters.pelatesTableAdapter
        Dim categorytable1 As hard_oneDataSet.pelatesDataTable = New hard_oneDataSet.pelatesDataTable

        categoryadapter1.Fill(categorytable1)
        ComboBoxPelates.DataSource = categorytable1
        ComboBoxPelates.DisplayMember = "idPelati"
        ComboBoxPelates.ValueMember = "idPelati"


        makeDataGrid()
    End Sub
    Private Function SaveChanges() As Boolean
        If ValidateData() Then
            Me.TableAdapterManager.UpdateAll(Me.Hard_oneDataSet)
            Return True
        End If
        Return False
    End Function

    Protected Overloads Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        AddHandler Me.ParentForm.FormClosing, AddressOf ParentForm_FormClosing
    End Sub
    Private Function ValidateData() As Boolean
        If Me.Validate() Then
            Try
                PoliseisBindingSource.EndEdit()
                Return True
            Catch ex As Exception
                If MessageBox.Show( _
                  "Invalid data was entered. Cancel edits?", _
                  "Poliseis", MessageBoxButtons.YesNo, _
                  MessageBoxIcon.Error) = DialogResult.Yes Then

                    PoliseisBindingSource.CancelEdit()
                End If
                Return False
            End Try
        End If
        Return False
    End Function
    Private Sub ParentForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
        If ValidateData() Then
            If Me.Hard_oneDataSet.poliseis.GetChanges _
            IsNot Nothing Then
                Dim response As Integer = _
                MessageBox.Show( _
                "Do you want to save your changes?", _
                "Changes have been made", _
                MessageBoxButtons.YesNoCancel, _
                MessageBoxIcon.Warning)
                If response = DialogResult.Yes Then
                    If SaveChanges() Then
                        MessageBox.Show("Your changes were saved", _
                        "Poliseis", MessageBoxButtons.OK)
                    Else
                        e.Cancel = True
                    End If
                ElseIf response = DialogResult.Cancel Then
                    e.Cancel = True
                End If
            End If
        End If

    End Sub

    Private Sub saveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveBtn.Click
        Dim counter = Convert.ToInt32(DataGridView1.Rows.Count.ToString)
        Me.PoliseisTableAdapter.InsertQuery(IdPoliseisTextBox.Text, ImerominiaDateTimePicker.Value.Date, Emporiki_katigoriaTextBox.Text, Convert.ToDouble(Sinoliki_timiTextBox.Text), Pelates_idPelatiTextBox.Text)
        For i As Integer = 0 To counter - 1
            Dim adapter As New hard_oneDataSetTableAdapters.poliseis_has_proiontaTableAdapter
            Dim table As New hard_oneDataSet.proiontaDataTable

            'MessageBox.Show(DataGridView1.Rows(i).Cells("proion").Value.ToString & "  " & IdAgorasTextBox.Text & "   " & DataGridView1.Rows(i).Cells("quant").Value)
            adapter.InsertPoliseis(IdPoliseisTextBox.Text, DataGridView1.Rows(i).Cells("proion").Value.ToString, DataGridView1.Rows(i).Cells("quant").Value)

        Next (i)

    End Sub

    Private Sub insertbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles insertbtn.Click
        DataGridView1.Rows.Add(New String() {ProiontaComboBox.SelectedValue.ToString, nameUnit.Text, PosotitaTextBox.Text, priceUnit.Text, (Convert.ToDouble(priceUnit.Text) * Convert.ToInt32(PosotitaTextBox.Text))})
        Dim counter = Convert.ToInt32(DataGridView1.Rows.Count.ToString)
        Dim temp = 0.0
        For i As Integer = 0 To counter - 1
            temp += Convert.ToDouble(DataGridView1.Rows(i).Cells("pricesinolo").Value.ToString)
        Next (i)
        Sinoliki_timiTextBox.Text = temp.ToString
    End Sub

    Private Sub ProiontaComboBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProiontaComboBox.Leave
        Dim adapter As New hard_oneDataSetTableAdapters.proiontaTableAdapter
        Dim table As New hard_oneDataSet.proiontaDataTable
        adapter.FillByProion(table, ProiontaComboBox.SelectedValue.ToString)

        priceUnit.Text = table.Rows(0).Item("timi_xondrikis").ToString()
        nameUnit.Text = table.Rows(0).Item("perigrafi").ToString()
    End Sub

    Private Sub stoixeiaForPrint()
        Dim adapter As New hard_oneDataSetTableAdapters.epixeirisiTableAdapter
        Dim table As New hard_oneDataSet.epixeirisiDataTable
        adapter.FillByStoixeiaEpixeirPrint(table, keepetaira)

        Dim adapterPelati As New hard_oneDataSetTableAdapters.pelatesTableAdapter
        Dim tablePelati As New hard_oneDataSet.pelatesDataTable
        adapterPelati.FillByPeletiPrint(tablePelati, Pelates_idPelatiTextBox.Text)

        Dim dataFromGrid As String = ""
        Dim counter = Convert.ToInt32(DataGridView1.Rows.Count.ToString)
        For i As Integer = 0 To counter - 1
            dataFromGrid += DataGridView1.Rows(i).Cells("quant").Value.ToString() & ","
            dataFromGrid += DataGridView1.Rows(i).Cells("proion").Value.ToString() & ","
            dataFromGrid += DataGridView1.Rows(i).Cells("price").Value.ToString() & ","
            dataFromGrid += DataGridView1.Rows(i).Cells("pricesinolo").Value.ToString() & "-"
        Next (i)

        'MessageBox.Show(table.Rows(0).Item("eponimia").ToString())
        'MessageBox.Show(table.Rows(0).Item("diefthinsi").ToString())
        'MessageBox.Show(table.Rows(0).Item("post_code").ToString())
        'MessageBox.Show(table.Rows(0).Item("poli").ToString())
        'MessageBox.Show(table.Rows(0).Item("tilefono").ToString())
        'MessageBox.Show(table.Rows(0).Item("afm"))
        'MessageBox.Show(tablePelati.Rows(0).Item("epitheto"))
        'MessageBox.Show(tablePelati.Rows(0).Item("epaggelma"))
        'MessageBox.Show(tablePelati.Rows(0).Item("diefthinsi"))
        'MessageBox.Show(tablePelati.Rows(0).Item("post_code"))
        'MessageBox.Show(tablePelati.Rows(0).Item("poli"))
        'MessageBox.Show(tablePelati.Rows(0).Item("phone"))
        'MessageBox.Show(tablePelati.Rows(0).Item("afm").ToString())
        'MessageBox.Show(dataFromGrid)
        'MessageBox.Show(Sinoliki_timiTextBox.Text)


        Dim printC As New printApodiksiC.Class1
        printC.fillApodiksi(table.Rows(0).Item("eponimia").ToString(), _
            " ", _
            table.Rows(0).Item("diefthinsi").ToString(), _
            table.Rows(0).Item("post_code").ToString(), _
            table.Rows(0).Item("poli").ToString(), _
            table.Rows(0).Item("tilefono").ToString(), _
            table.Rows(0).Item("afm").ToString(), _
            tablePelati.Rows(0).Item("epitheto").ToString(), _
            tablePelati.Rows(0).Item("epaggelma").ToString(), _
            tablePelati.Rows(0).Item("diefthinsi").ToString(), _
            tablePelati.Rows(0).Item("post_code").ToString(), _
            tablePelati.Rows(0).Item("poli").ToString(), _
            tablePelati.Rows(0).Item("phone").ToString(), _
            tablePelati.Rows(0).Item("afm").ToString(), dataFromGrid, Sinoliki_timiTextBox.Text)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        stoixeiaForPrint()
    End Sub
End Class
